package com.txw.javaCore.server.controller;

import cn.hutool.core.util.RandomUtil;
import com.txw.javaCore.api.response.BaseResponse;
import com.txw.javaCore.api.response.StatusCode;
import com.txw.javaCore.model.entity.UserOrder;
import com.txw.javaCore.model.request.OrderCancelRequest;
import com.txw.javaCore.server.service.OrderService;
import com.txw.javaCore.server.util.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION: 商城平台用户订单实战控制器层
 * @Author: 涂玄武
 * @DATE: 2021/1/29 16:49
 */
@RestController
@RequestMapping(value = "order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    /**
     * 下订单
     * @param order
     * @param result
     * @return
     */
    @RequestMapping(value = "push")
    public BaseResponse pushOrder(@RequestBody @Validated UserOrder order, BindingResult result) {
        //参数检核
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)){
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(orderService.addOrder(order));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }

    /**
     * 订单详情
     *
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "info")
    public BaseResponse getOrder(@RequestParam String orderNo) {
        //参数检核
        if (StringUtils.isBlank(orderNo)){
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(orderService.getOrderInfo(orderNo));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }

    /**
     * 下订单V2
     * @param order
     * @param result
     * @return
     */
    @RequestMapping(value = "push/v2")
    public BaseResponse pushOrderV2(@RequestBody @Validated UserOrder order,BindingResult result) {
        //参数检核
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)){
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            long ttl = RandomUtil.getRandom().nextLong(10, 20);
            response.setData(orderService.addOrderV2(order,ttl));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }

    /**
     * 取消订单
     * @param request
     * @param result
     * @return
     */
    @RequestMapping(value = "cancel", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BaseResponse cancelOrder(@RequestBody @Validated OrderCancelRequest request, BindingResult result) {
        //参数检核
        String checkRet = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRet)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(), checkRet);
        }
        log.info("--前端接受的请求参数：{}", request);
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {

        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }
}
